import { message } from 'antd';
import { history } from 'umi';
import { addLesson,updateLesson,fetchCoach,fetchLesson,queryMemberCardInfoByCourseId,fetchShopList,queryAllCardByCourseId,selectAllCourse} from './service';

const Model = {
  namespace: 'cubeLesson',
  state: {
    coachList:[],
    coachOptions:{},
    lesson:{},
    cardList:[],
    shopList:[],
    newCourseId:"",
    courseList:[]
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addLesson, payload);
      if(result.code===0){
        message.success('成功新增课程...');
        return result.data
      }else{
        message.error(result.message);
        return null
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateLesson, payload);
      if(result.code===0){
        message.success('成功编辑课程...');
        return true
      }else{
        message.error(result.message);
        return false
      }
    },
    *fetchCoach({ payload }, { call,put }) {
      let result=yield call(fetchCoach, payload);
      if(result.code===0){
        let _coachOptions={}
        if(result.data){
          result.data.map((item)=>{
            _coachOptions[item.userId]=item.name
          })
        }
        yield put({
          type: 'common',
          payload: {
            coachList:result.data,
            coachOptions:_coachOptions
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchLesson({ payload }, { call,put }) {
      let result=yield call(fetchLesson, payload);
      if(result.code===0){
        if(result.data.lableList){
          let lableList=result.data.lableList
          result.data.lableList=[]
          lableList.map((item)=>{
            result.data.lableList.push(item.labelName)
          })
        }
        if(result.data.isStartUpClass=='1'){
          result.data.isStartUpClass=true
        }else{
          result.data.isStartUpClass=false
        }
        yield put({
          type: 'common',
          payload: {
            lesson:result.data
          }
        });
        return result.data
      }else{
        message.error(result.message);
      }
    },
    *queryMemberCardInfoByCourseId({ payload }, { call,put }) {
      let result=yield call(queryMemberCardInfoByCourseId, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            cardList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchShopList({ payload }, { call,put }) {
      let result=yield call(fetchShopList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            shopList:result.data         
           }
        });
        return result.data   
      }else{
        message.error(result.message);
      }
    },
    *queryAllCardByCourseId({ payload }, { call,put }) {
      let result=yield call(queryAllCardByCourseId, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            allCardByCourseId:result.data
          }
        });
        return result.data
      }else{
        message.error(result.message);
      }
    },
    *selectAllCourse({ payload }, { call,put }) {
      let result=yield call(selectAllCourse, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            courseList:result.data         
           }
        });
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
